package com.hwz.gulimall.member.controller;

import java.util.Arrays;
import java.util.Map;

import com.hwz.common.entity.OAuthToken;
import com.hwz.common.exception.BizCodeEnume;
import com.hwz.gulimall.member.entity.MemberLevelEntity;
import com.hwz.gulimall.member.entity.UserLoginVo;
import com.hwz.gulimall.member.entity.UserRegisterVo;
import com.hwz.gulimall.member.exception.UserEmailExistException;
import com.hwz.gulimall.member.exception.UserNameExistException;
import com.hwz.gulimall.member.feign.CouponService;
import com.hwz.gulimall.member.service.MemberLevelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import com.hwz.gulimall.member.entity.MemberEntity;
import com.hwz.gulimall.member.service.MemberService;
import com.hwz.common.utils.PageUtils;
import com.hwz.common.utils.R;



/**
 * 会员
 *
 * @author hwz
 * @email 1281561921@qq.com
 * @date 2025-05-28 13:25:49
 */@RefreshScope
@RestController
@RequestMapping("member/member")
@Slf4j
public class MemberController {
    @Autowired
    private MemberService memberService;
    @Autowired
    private CouponService couponService;

    @Autowired
    private MemberLevelService memberLevelService;
    @Value("${tag}")
    private String tag;
    @RequestMapping("/getCoupon")
    public R test(@RequestParam Map<String, Object> params){
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setNickname("小黑");

        System.out.println(tag);

        R r = couponService.memberList();


        return R.ok().put("member", memberEntity).put("coupon",r.get("coupons"));
    }



    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }



    /**
     * 注册
     */
    @RequestMapping("/regist")
    public R regist(@RequestBody UserRegisterVo userRegisterVo){
        MemberEntity member = new MemberEntity();


        MemberLevelEntity memberLevelEntity = memberLevelService.getDefaultLevel();
        member.setLevelId(memberLevelEntity.getId());
        try {
            //校验用户是否已经存在
            memberService.checkUserNameExist(userRegisterVo.getUsername());
            memberService.checkUserEmailExist(userRegisterVo.getEmail());
        } catch (UserNameExistException e) {
            log.error(e.getMessage());
            return R.error(BizCodeEnume.USER_EXIST_EXCEPTION.getCode(), BizCodeEnume.USER_EXIST_EXCEPTION.getMsg());
        }catch (UserEmailExistException e){
            log.error(e.getMessage());
            return R.error(BizCodeEnume.EMAIL_EXIST_EXCEPTION.getCode(), BizCodeEnume.EMAIL_EXIST_EXCEPTION.getMsg());
        }
        member.setUsername(userRegisterVo.getUsername());
        member.setEmail(userRegisterVo.getEmail());


        //密码加密
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        member.setPassword(bCryptPasswordEncoder.encode(userRegisterVo.getPassword()));


        memberService.getBaseMapper().insert(member);

        return R.ok();
    }

    @PostMapping("/login")
    public R login(@RequestBody UserLoginVo userLoginVo){
        MemberEntity member = memberService.login(userLoginVo);
        if (member != null){
            return R.ok().setData(member);
        }else {
            return R.error(BizCodeEnume.USER_LOGIN_FAIL_EXCEPTION.getCode(), BizCodeEnume.USER_LOGIN_FAIL_EXCEPTION.getMsg());
        }

    }

    @PostMapping("oauth/login")
    public R oauthLogin(@RequestBody OAuthToken oAuthToken){
        MemberEntity member = memberService.oauthLogin(oAuthToken);
        if (member != null){
            return R.ok().setData(member);
        }else {
            return R.error(BizCodeEnume.OAUTH_LOGIN_EXCEPTION.getCode(), BizCodeEnume.OAUTH_LOGIN_EXCEPTION.getMsg());
        }

    }

}
